package com.lwz.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.lwz.eduservice.entity.EduSubject;
import com.lwz.eduservice.entity.excel.SubjectData;
import com.lwz.eduservice.entity.subject.OneSubject;
import com.lwz.eduservice.entity.subject.TwoSubject;
import com.lwz.eduservice.listener.SubjectExcelListener;
import com.lwz.eduservice.mapper.EduSubjectMapper;
import com.lwz.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author Lw中
 * @since 2020-11-10
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void addSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            // 获取文件输入流
            InputStream inputStream = file.getInputStream();
            // 调用方法浸信读取
            EasyExcel.read(inputStream, SubjectData.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {

        // 查询一级分类（parent_id为零）
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        // 查询二级分类（parent_id不为零）
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", "0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        // 创建list用于封装最终结果
        List<OneSubject> finalSubjectList = new ArrayList<>();

        // 通过双重for循环遍历封装一二级分类
        for (int i = 0; i < oneSubjectList.size(); i++) {
            EduSubject eduSubject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
            // 将获取的一级分类放到oneSubject中
            BeanUtils.copyProperties(eduSubject, oneSubject);
            // 将多个一级分类放到最终封装结果中
            finalSubjectList.add(oneSubject);

            // 在一级分类遍历中遍历所有的二级分类
            // 创建集合保存每个一级分类的二级分类
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            for (int j = 0; j < twoSubjectList.size(); j++) {
                //获取每个二级分类
                EduSubject tSubject = twoSubjectList.get(j);
                // 判断二级分类中的parent_id是否等于一级分类id
                if (tSubject.getParentId().equals(eduSubject.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    // 将所属二级分类中的类复制到真正的二级分类中
                    BeanUtils.copyProperties(tSubject, twoSubject);
                    // 将二级分类添加到twoFinalSubjectList
                    twoFinalSubjectList.add(twoSubject);
                }
            }
            // 将每次遍历到的二级分类放到一级分类中去
            oneSubject.setChildren(twoFinalSubjectList);
        }

        return finalSubjectList;

    }


}
